package com.example.jjc.rxjava_retrofit2_demo;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.jjc.rxjava_retrofit2_demo.adapter.ImagePageAdapter;
import com.example.jjc.rxjava_retrofit2_demo.adapter.ZhuangbiListAdapter;
import com.example.jjc.rxjava_retrofit2_demo.model.Item;
import com.example.jjc.rxjava_retrofit2_demo.module.Fra_1;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/17.
 */

public class ImageActivity extends AppCompatActivity {

//    @BindView(R.id.iv_big)
//    ImageView ivBig;
    @BindView(R.id.rv_image)
    RecyclerView rv_image;
    @BindView(R.id.vp_image)
    ViewPager vpImage;

    private Uri uri;//要显示的图片uri
    private List<Item> list = null;
    private int cur_position = 0;//当前位置
    private View pre_view;//上一个视图

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        ButterKnife.bind(this);
        uri = getIntent().getData();
        Log.d("Fra", uri.toString());
        initView();
    }

    private void initView() {
        //配置recyclerView
        final LinearLayoutManager llmanager = new LinearLayoutManager(this);
        llmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_image.setLayoutManager(llmanager);
        String tag = BaseFragment.FRA_TAG_1;
        final ZhuangbiListAdapter adapter = new ZhuangbiListAdapter();
        final Fra_1 fra_1 = (Fra_1) MainActivity.getInstance().getFragmentManager().findFragmentByTag(tag);
        adapter.setImageList(fra_1.getList());
        list = adapter.getImageList();
        adapter.setHorizontal(true);
        rv_image.setAdapter(adapter);
        rv_image.scrollToPosition(fra_1.getPosition());//点击的item完全显示出来

        //填充viewPager
        ImagePageAdapter imagePageAdapter = new ImagePageAdapter(ImageActivity.this,list);
        vpImage.setAdapter(imagePageAdapter);
        vpImage.setCurrentItem(fra_1.getPosition());
//        pre_view = rv_image.findViewHolderForAdapterPosition(fra_1.getPosition()).itemView;
//        pre_view.setBackgroundColor(Color.RED);

        //点击效果
        adapter.setOnItemClickListener(new ZhuangbiListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view) {
                cur_position = rv_image.getChildAdapterPosition(view);
//                llmanager.scrollToPositionWithOffset(rv_image.getChildAdapterPosition(view), (rv_image.getWidth()-view.getWidth())/2);//点击的item居中
                rv_image.scrollToPosition(cur_position);//点击的item完全显示出来

                vpImage.setCurrentItem(cur_position);
                //选择变色
                if (pre_view != null){
                    pre_view.setBackgroundColor(Color.WHITE);
                }
                pre_view = view;
                view.setBackgroundColor(Color.RED);
            }
        });
    }
}
